package com.shusl.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shusl.web.*;
import com.shusl.exception.ParameterException;
import com.shusl.entity.User;
import com.shusl.mapper.UserMapper;
import com.shusl.service.IUserService;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public PageResult<User> list(PageParam<User> page) {
        return new PageResult<>(this.page(page).getRecords(), page.getTotal());
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        User user = baseMapper.selectById(id);
        return JsonResult.ok().setData(user);
    }

    @Override
    public JsonResult add(User user) {
        if (user ==null){
            throw new ParameterException("参数对象为空");
        }

        baseMapper.insert(user);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(User user) {
        if (user == null) {
            throw new ParameterException("参数对象为空");
        }

        if (user.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(user);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}